<template>
  <el-dialog
    class="export-seal-use-record-dialog"
    title="导出印章使用记录"
    width="400px"
    :visible.sync="dialogVisible"
    :close-on-click-modal="true"
    v-dialogDrag="true"
    append-to-body
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <el-form ref="exportSealUseRecordFormRef" :model="formData" :rules="formRules" size="small" label-width="100px">
      <el-form-item label="使用日期" prop="useDate" label-width="80px">
        <el-date-picker
          style="width: 100%;"
          v-model="formData.useDate"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :unlink-panels="true"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="使用人" prop="userId" label-width="80px">
        <el-select
          style="width: 100%;"
          v-model="formData.userId"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
          </el-select>
      </el-form-item>
    </el-form>

    <div class="dialog-footer" slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="handleConfirm">确 定</el-button>
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listUser } from '@/api/system/user'
import { exportSealUseRecord } from "@/api/testItem/detectionResult";

export default {
  name: 'ExportSealUseRecordDialog',
  components: {},
  dicts: [],
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      userList: [],
      formData: {},
      formRules: {},
      btnLoading: false,
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 弹框打开时
    handleDialogOpen() {
      this.getUserList()
    },
    // 弹框关闭时
    handleDialogClosed() {
      this.initFormData()
      this.$refs.exportSealUseRecordFormRef.resetFields()
    },
    // 获取用户列表
    getUserList() {
      if (this.userList.length <= 0) {
        listUser({ pageNum: 1, pageSize: 10000 }).then(res => {
          this.userList = res.rows || []
        }).catch(err => {})
      }
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        useDate: [],
        userId: '',
      }
    },
    // 获取表单数据
    getFormData() {
      const data = JSON.parse(JSON.stringify(this.formData))
      data.beginDate = data.useDate ? data.useDate[0] : ''
      data.endDate = data.useDate ? data.useDate[1] : ''
      delete data.useDate
      return data
    },
    // 点击确定
    handleConfirm() {
      this.$refs.exportSealUseRecordFormRef.validate(valid => {
        if (!valid) return
        const data = this.getFormData()
        console.log('表单数据', data)
        this.btnLoading = true
        exportSealUseRecord(data).then(async res => {
          console.log('导出结果', res)
          this.btnLoading = false
          if (res.type == 'application/json') {
            const json = await res.text()
            const result = JSON.parse(json)
            if (result.code !== 200) {
              this.$message.error(result.msg)
              return
            }
          }
          this.$message.success('操作成功')
          const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' })
          const downloadUrl = URL.createObjectURL(blob)
          const aLink = document.createElement('a')
          aLink.href = downloadUrl
          aLink.download = '签章使用记录.docx'
          aLink.click()
          this.dialogVisible = false
        }).catch(err => {
          this.btnLoading = false
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.export-seal-use-record-dialog {

  ::v-deep .el-dialog__body {
    max-height: 70vh;
    overflow-y: auto;
  }

  ::v-deep .transfer {
    width: 100%;

    .el-transfer-panel {
      width: calc(calc(100% - 182px) / 2);
    }

    .el-transfer-panel__body {
      height: calc(70vh - 40px - 100px - 15px);
    }

    .el-transfer-panel__list {
      height: calc(100% - 32px - 15px);
    }
  }
}
</style>
